package com.huluobo.appbase.tools

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.huluobo.appbase.R
import com.huluobo.appbase.utils.DeviceUtil

/**
 * Created by lc on 2022/12/29.
 */
class LoadingDialog private constructor(mContext: Context) : Dialog(mContext), LifecycleObserver {

    private lateinit var tv_bottom_desc: TextView
    private lateinit var ll_dialog: LinearLayout
    private lateinit var progressBar: ProgressBar
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.item_dialog_loading)
        tv_bottom_desc = findViewById(R.id.tv_bottom_desc)
        ll_dialog = findViewById(R.id.ll_dialog)
        progressBar = findViewById(R.id.progressBar)

        val dialogWindow = window
        dialogWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    /**
     * 是否可以触摸取消,默认为false
     */
    private var canceledOnTouchOutsideValue = false

    /**
     * 是否可以点击返回取消,默认为false
     */
    private var canceledOnBack = false

    /**
     * 背景框默认
     */
    private var dialogBackgroundResource = R.drawable.drawable_loading_toast

    /**
     * 底部文字的颜色
     */
    private var bottomTextColor = mContext.resources.getColor(R.color.white)

    /**
     * 底部文字大小
     */
    private var bottomTextSize = 15f

    /**
     * 弹窗布局的大小 宽度
     */
    private var dialogWidth = 0f

    /**
     * 天窗布局的大小 高度
     */
    private var dialogHeight = 0f

    /**
     * progressbar大小
     */
    private var progressbarSize = 0f

    /**
     * 底部文字描述
     */
    private var bottomDesc: String? = null

    @Override
    override fun show() {
        if (ownerActivity != null && !ownerActivity!!.isFinishing) {
            super.show()
            show(this)
        }
    }

    private fun show(loadingDialog: LoadingDialog) {
        if (!TextUtils.isEmpty(bottomDesc)) {
            tv_bottom_desc.text = bottomDesc
            tv_bottom_desc.visibility = View.VISIBLE
        }

        //屏蔽返回键
        if (!canceledOnBack) {
            setOnKeyListener { dialog, keyCode, event ->
                keyCode == KeyEvent.KEYCODE_BACK
            }
        }
        //设置触摸外部取消dialog
        setCanceledOnTouchOutside(canceledOnTouchOutsideValue)

        //设置背景
        ll_dialog.setBackgroundResource(dialogBackgroundResource)

        //设置字体颜色
        tv_bottom_desc.setTextColor(bottomTextColor)

        //设置字体大小
        tv_bottom_desc.textSize = bottomTextSize

        if (dialogWidth != 0f) {
            val layoutParams = ll_dialog.layoutParams
            layoutParams.width = DeviceUtil.dp2px(context, dialogWidth)
            layoutParams.height = DeviceUtil.dp2px(context, dialogHeight)
            ll_dialog.layoutParams = layoutParams
        }

        if (progressbarSize != 0f) {
            val layoutParams = ll_dialog.layoutParams
            layoutParams.width = DeviceUtil.dp2px(context, progressbarSize)
            layoutParams.height = DeviceUtil.dp2px(context, progressbarSize)
            progressBar.layoutParams = layoutParams
        }
    }

    /**
     * 使用构建者模式创建Dialog,重点:返回的都是对象本身
     */
    class Builder(context: Context) {
        private var loadingDialog: LoadingDialog = LoadingDialog(context)

        /**
         * 设置底部文字描述
         */
        fun setBottomDesc(bottomDesc: String): Builder {
            loadingDialog.bottomDesc = bottomDesc
            return this
        }

        /**
         * 设置触摸屏幕是否消失
         */
        fun setCanceledOnTouchOutsideValue(canceled: Boolean): Builder {
            loadingDialog.canceledOnBack = canceled
            return this
        }

        /**
         * 设置是否可以点击返回键
         */
        fun setCanceledOnBackValue(onBack: Boolean): Builder {
            loadingDialog.canceledOnBack = onBack
            return this
        }

        /**
         * 设置dialog背景
         */
        fun setDialogBackground(resource: Int): Builder {
            loadingDialog.dialogBackgroundResource = resource
            return this
        }

        /**
         * 设置底部字体颜色
         */
        fun setBottomTextColor(resource: Int): Builder {
            loadingDialog.bottomTextColor = resource
            return this
        }

        /**
         * 设置底部文字的大小
         */
        fun setBottomTextSize(size: Float): Builder {
            loadingDialog.bottomTextSize = size
            return this
        }

        /**
         * 设置弹窗的大小
         */

        fun setDialogSize(dialogWidth: Float, dialogHeight: Float): Builder {
            loadingDialog.dialogWidth = dialogWidth
            loadingDialog.dialogHeight = dialogHeight
            return this
        }

        /**
         * 设置progressbar大小
         */
        fun setProgressBarSize(size: Float): Builder {
            loadingDialog.progressbarSize = size
            return this
        }

        /**
         * 通过Builder设置完属性后构造对话框的方法
         */
        fun create(): LoadingDialog {
            return loadingDialog
        }
    }

    /**
     * 使用Lifecycle自动销毁弹窗
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        if (isShowing) {
            dismiss()
        }
    }

    init {
        if (mContext is Activity) {
            setOwnerActivity(mContext)
        }

        if (mContext is ComponentActivity) {
            mContext.lifecycle.addObserver(this)
        }
    }
}